package com.ysstech.kettle.controller;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.enums.YssEnum;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.fileutil.FileWriterUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.*;
import com.ysstech.kettle.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-11
 */
@Controller
@Slf4j
@Api(tags = "任务执行api")
@RequestMapping("/jobExecute")
public class JobExecuteController extends BaseController {

    @Autowired
    private JobExecuteService jobExecuteService;


    /**
     * 执行作业分类信息
     *
     * @return
     */
    @ApiOperation(value = "执行作业分类信息")
    @YssLog(module = "任务执行", remark = "执行作业分类信息")
    @RequestMapping(value = "/executeJobCategory")
    @ResponseBody
    public RestResult executeJobCategory(String ids, String param) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取执作业分类信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        List<JobParam> listParam = null;
        if (StringUtils.isNotBlank(param)) {
            listParam = JSONArray.parseArray(param, JobParam.class);
        }
        jobExecuteService.execute(list, listParam, false, false);
        return ResultGenerator.getSuccessResult("执行作业分类信息成功！");
    }

    /**
     * 执行作业任务信息
     *
     * @return
     */
    @ApiOperation(value = "执行作业任务信息")
    @YssLog(module = "任务执行", remark = "执行作业任务信息")
    @RequestMapping(value = "/executeJobTask")
    @ResponseBody
    public RestResult executeJobTask(String ids, String param) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取执行作业任务信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        List<JobParam> listParam = null;
        if (StringUtils.isNotBlank(param)) {
            listParam = JSONArray.parseArray(param, JobParam.class);
        }
        jobExecuteService.execute(list, listParam, false, true);
        return ResultGenerator.getSuccessResult("执行作业任务信息成功！");
    }

    /**
     * 执行作业任务明细信息
     *
     * @return
     */
    @ApiOperation(value = "执行作业任务明细信息")
    @YssLog(module = "任务执行", remark = "执行作业任务明细信息")
    @RequestMapping(value = "/executeJobTaskDetail")
    @ResponseBody
    public RestResult executeJobTaskDetail(String ids, String param) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取执行作业任务明细信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        List<JobParam> listParam = null;
        if (StringUtils.isNotBlank(param)) {
            listParam = JSONArray.parseArray(param, JobParam.class);
        }
        jobExecuteService.execute(list, listParam, true, false);
        return ResultGenerator.getSuccessResult("执行作业任务明细信息成功！");
    }

    @Value("${file.uploadFolder}")
    private String path;

    /**
     * 导出kettle文件
     *
     * @return
     */
    @ApiOperation(value = "导出kettle文件")
    @YssLog(module = "任务执行", remark = "导出kettle文件")
    @RequestMapping(value = "/exportTemplateXml")
    @ResponseBody
    public RestResult exportTemplateXml(HttpServletResponse response) throws Exception {
        String fileName = "KETTLE_EXPORT_" + DateUtil.localDateToStrByFormat(LocalDate.now(), YssEnum.YSS_DEFAULT_DATE_FORMAT.getCode());
        jobExecuteService.exportTemplateXml(path, fileName, null);
        FileWriterUtil.downloadFile(response, path + fileName);
        return ResultGenerator.getSuccessResult("导出kettle文件成功！");
    }

    /**
     * 导入kettle文件
     *
     * @return
     */


    @ApiOperation(value = "导入kettle文件")
    @YssLog(module = "任务执行", remark = "导入kettle文件")
    @RequestMapping(value = "/importTemplateXml")
    @ResponseBody
    public RestResult importTemplateXml(MultipartFile multipartFile) throws Exception {
        if (multipartFile.isEmpty()) {
            throw new BusinessException("导入文件为空，请稍后重试！");
        }
        File tempFile = FileUtil.createTempFile(new File(path));
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), tempFile);
        jobExecuteService.importTemplateXml(path, tempFile.getName());
        return ResultGenerator.getSuccessResult("导入kettle文件成功！");
    }
}
